<template>
  <div>
    <el-breadcrumb separator="/" separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{}">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-button type="primary" size="medium" @click="add">添加角色</el-button>

      <el-table :data="arr" style="width: 100%"  :header-cell-style="{background:'#EEF1F6',color:'#606266'}">
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-row
              :class="{ vcenter: index != props.row.children.length - 1 }"
              v-for="(item, index) in props.row.children"
              :key="item.id"
            >
              <el-col :span="6">
                <el-tag>{{ item.authName }}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span="18">
                <el-row v-for="item1 in item.children" :key="item1.id">
                  <el-col :span="6">
                    <el-tag
                      type="success"
                      @close="danDel(props.row, item1.id)"
                      closable
                      >{{ item1.authName }}</el-tag
                    >
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                    <el-tag
                      type="warning"
                      @close="danDel(props.row, item2.id)"
                      v-for="item2 in item1.children"
                      :key="item2.id"
                      closable
                      >{{ item2.authName }}
                    </el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column label="#" type="index"> </el-table-column>
        <el-table-column label="角色名称" prop="roleName"> </el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              @click="update(scope.row)"
            ></el-button>
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="del(scope.row.id)"
            ></el-button>
            <el-tooltip
              class="item"
              effect="dark"
              content="分配权限"
              placement="top"
            >
              <el-button
                size="mini"
                type="warning"
                icon="el-icon-setting"
                @click="role(scope.row)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog :title="title" :visible.sync="dialogVisible" width="40%">
      <el-form label-position="left" :model="list" label-width="80px">
        <el-form-item label="角色名称">
          <el-input v-model="list.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="list.roleDesc"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="quedin(list)">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog title="分配权限" :visible.sync="dialogVisible2" width="40%">
      <el-tree
        ref="tree"
        :data="treeList"
        show-checkbox
        node-key="id"
        :default-expand-all="true"
        @check="change"
        :default-checked-keys="rolesArr"
        :props="{ label: 'authName' }"
      >
      </el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible2 = false">取 消</el-button>
        <el-button type="primary" @click="roles(list)">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      arr: [],
      treeList: [],
      rolesArr: [],
      checkRoles: [],
      roleId: 0,
      list: {
        roleName: "",
        roleDesc: "",
      },
      title: "",
      flag: "",
      dialogVisible: false,
      dialogVisible2: false,
    };
  },
  methods: {
    //删除标签 删除角色指定权限
    async danDel(row, id) {
      var res = await this.$http.delete(`roles/${row.id}/rights/${id}`);
      // hint(res);
      row.children = res.data.data;
      // console.log(row.children);
      // console.log(res.data.data)
    },
    //改变分配权限 change 获取当前权限对应的id
    change() {
      this.checkRoles = [
        ...this.$refs.tree.getHalfCheckedKeys(),
        ...this.$refs.tree.getCheckedKeys(),
      ];
      // this.$refs.tree.getHalfCheckedKeys();
      // console.log(this.checkRoles);
      // console.log(this.$refs.tree.getHalfCheckedKeys());
      // console.log(b.checkedKeys);
    },
    //分配权限按钮
    async role(row) {
      this.roleId = row.id;
      this.dialogVisible2 = true;
      var res = await this.$http.get("rights/tree", {});
      this.rolesArr = [];
      this.treeList = res.data.data;
      row.children.forEach((item1) => {
        item1.children.forEach((item2) => {
          item2.children.forEach((item3) => {
            this.rolesArr.push(item3.id);
            console.log(item3.id);
          });
        });
      });
      // console.log(this.rolesArr);
    },
    //分配权限弹框 确定按钮
    async roles() {
      var res = await this.$http.post(`roles/${this.roleId}/rights`, {
        rids: this.checkRoles.join(","),
      });
      this.hint(res);
      // console.log(res)
      this.dialogVisible2 = false;
    },
    // 添加按钮
    add() {
      this.dialogVisible = true;
      this.title = "添加角色";
      for (const key in this.list) {
        this.list[key] = "";
      }
      this.flag = true;
    },
    //修改按钮
    update(row) {
      this.dialogVisible = true;
      this.title = "修改角色";
      Object.assign(this.list, row);
      this.flag = false;
    },
    //添加 修改 确定按钮
    quedin() {
      //添加角色
      if (this.flag == true) {
        this.$http.post("roles", this.list).then((res) => {
          this.hint(res);
          this.dialogVisible = false;
        });
      } else {
        //编辑角色
        this.$http.put(`roles/${this.list.id}`, this.list).then((res) => {
          this.hint(res);
          this.dialogVisible = false;
        });
      }
    },
    //删除角色
    async del(id) {
      this.$http.delete(`roles/${id}`, {}).then((res) => {
        this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(() => {
            this.hint(res);
          }).catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          });
      });
    },
    //封装 成功失败 提示消息
    hint(res) {
      if (res.data.meta.status == 200 || res.data.meta.status == 201) {
        this.$message({
          showClose: true,
          message: res.data.meta.msg,
          type: "success",
        });
        this.getDate();
      } else {
        this.$message({
          showClose: true,
          message: res.data.meta.msg,
          type: "error",
        });
      }
    },
    //角色列表数据
    async getDate() {
      var { data: res } = await this.$http.get("roles", {});
      this.arr = res.data;
    },
  },
  created() {
    this.getDate();
  },
};
</script>

<style>
.el-breadcrumb {
  margin-bottom: 20px;
}

.vcenter {
  display: flex;
  align-items: center;
  border-bottom: 1px solid #eee;
}

.el-tag {
  margin: 5px;
}
</style>